#ifndef SPL_DWM
#define SPL_DWM

#define DWMAPI_NAME _T("dwmapi.dll")

class DLLEXPORT DWMManager
{
private:
  HMODULE hModule;

  typedef HRESULT (WINAPI *DWMENABLED)(__out BOOL*);
  typedef HRESULT (WINAPI *DWMEXTEND)(HWND, __in const PMARGINS);

  DWMENABLED pCompositionEnabled;
  DWMEXTEND pExtendFrame;

public:
  SPLINL BOOL isLoaded();
  SPLINL void isCompositionEnabled(BOOL *fEnabled);
  SPLINL void extendFrame(HWND hWnd, const PMARGINS pMargins);

  SPLINL DWMManager();
  SPLINL ~DWMManager();
};

DWMManager::DWMManager() :
pCompositionEnabled(NULL), pExtendFrame(NULL), hModule(NULL)
{
  if (GetVersion() >= 0x0600)
  {
    // load dwmapi.dll library
    hModule = LoadLibrary(DWMAPI_NAME);
  }
}

DWMManager::~DWMManager()
{
  // free dwmapi.dll library
  if (hModule)
    FreeLibrary(hModule);
}

BOOL DWMManager::isLoaded()
{
  // check if dwmapi.dll module handle is valid
  return hModule != NULL;
}

void DWMManager::isCompositionEnabled(BOOL *fEnabled)
{
  // if function address not stored
  if (!pCompositionEnabled)
  {
    // get function address
    pCompositionEnabled = (DWMENABLED)GetProcAddress(
      hModule, "DwmIsCompositionEnabled");
  }

  // if valid function address, get composition state
  if (pCompositionEnabled)
    pCompositionEnabled(fEnabled);
}

void DWMManager::extendFrame(HWND hWnd, const PMARGINS pMargins)
{
  // if function address not stored
  if (!pExtendFrame)
  {
    // get function address
    pExtendFrame = (DWMEXTEND)GetProcAddress(
      hModule, "DwmExtendFrameIntoClientArea");
  }

  // if valid function address, set frame margins
  if (pExtendFrame)
    pExtendFrame(hWnd, pMargins);
}

#endif